package com.jinbo.footmessage.serverInf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import com.jinbo.footmessage.BaseActivity;
import com.jinbo.footmessage.MyApplication;
import com.jinbo.footmessage.R;
import com.jinbo.footmessage.baiduMap.SelectAddressActivity;
import com.jinbo.footmessage.utils.CropImageUtils;
import com.jinbo.footmessage.utils.LocalPreferencesHelper;
import com.jinbo.footmessage.view.CrossEditText;
import com.jinbo.footmessage.view.TitleBarView;

public class EditOrderInfActivity extends BaseActivity implements View.OnClickListener {
    private static final int REQUEST_MAP = 100;


    private static String NAME_INTENT = "NAME";
    private static String PHONE_INTENT = "PHONE";
    private static String ADDRESS_INTENT = "ADDRESS";
    private static String SEX_INTENT = "SEX";

    public static void launch(Activity activity, String name, String phone, String address, String sex,int code) {
        Intent intent = new Intent(activity, EditOrderInfActivity.class);
        intent.putExtra(NAME_INTENT, name);
        intent.putExtra(PHONE_INTENT, phone);
        intent.putExtra(ADDRESS_INTENT, address);
        intent.putExtra(SEX_INTENT, sex);
        activity.startActivityForResult(intent, code);
    }

    Button mSubmitBt;
    CrossEditText mContactNameEt, mContactPhoneEt;
    EditText mContactAddressEt;
    RadioGroup mContactSexRg;

    String mSexStr = "男";


    LinearLayout mLocLL;


    double latitude =-1, longitude=-1;
    String address;
   // String mAddress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_order_inf);

        mSubmitBt = (Button) findViewById(R.id.submit);
        mSubmitBt.setOnClickListener(this);
        mContactNameEt = (CrossEditText) findViewById(R.id.contact_name);
        mContactNameEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                check();
            }
        });
        mContactPhoneEt = (CrossEditText) findViewById(R.id.contact_phone);
        mContactPhoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                check();
            }
        });
        mContactAddressEt = (EditText) findViewById(R.id.contact_address);
        mContactAddressEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                check();
            }
        });
        mContactSexRg = (RadioGroup) findViewById(R.id.contact_sex);
        Intent intent = getIntent();
        mContactNameEt.setText(intent.getStringExtra(NAME_INTENT));
        int length1 = mContactNameEt.getText().length();
        Selection.setSelection(mContactNameEt.getText(), length1);
        mContactPhoneEt.setText(intent.getStringExtra(PHONE_INTENT));
        int length2 = mContactPhoneEt.getText().length();
        Selection.setSelection(mContactPhoneEt.getText(), length2);
        mContactAddressEt.setText(intent.getStringExtra(ADDRESS_INTENT));
        int length3 = mContactAddressEt.getText().length();
        Selection.setSelection(mContactAddressEt.getText(), length3);
        if (intent.getStringExtra(SEX_INTENT).equals("女"))
            mContactSexRg.check(R.id.female);
        else
            mContactSexRg.check(R.id.male);
        mContactSexRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                mSexStr = checkedId == R.id.male ? "男" : "女";
            }
        });

        TitleBarView titleBarView = (TitleBarView) findViewById(R.id.titleBar);
        titleBarView.setCenterTitle("编辑信息");
        titleBarView.setLeftBackButtonIsVisibler(true);
        titleBarView.setListener(TitleBarView.TitleBarEnum.ll_leftBackImageLayout, this);

        mLocLL = (LinearLayout) findViewById(R.id.baidu_map);
        mLocLL.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.left_back_layout:
                finish();
                break;
            case R.id.submit:
                MyApplication.local.saveOrUpdate(LocalPreferencesHelper.CONTACT_NAME, mContactNameEt.getText().toString());
                MyApplication.local.saveOrUpdate(LocalPreferencesHelper.CONTACT_PHONE, mContactPhoneEt.getText().toString());
                MyApplication.local.saveOrUpdate(LocalPreferencesHelper.CONTACT_ADDRESS, mContactAddressEt.getText().toString());
                MyApplication.local.saveOrUpdate(LocalPreferencesHelper.CONTACT_SEX, mSexStr);
                Intent intent = new Intent();
                intent.putExtra("name", mContactNameEt.getText().toString());
                intent.putExtra("phone", mContactPhoneEt.getText().toString());
                intent.putExtra("address", mContactAddressEt.getText().toString());
                intent.putExtra("sex", mSexStr);
                intent.putExtra("latitude", latitude );
                intent.putExtra("latitude", longitude);
                // 设置结果，并进行传送
                setResult(RESULT_OK, intent);
                finish();
                break;
            case R.id.baidu_map:
                    SelectAddressActivity.launch(this,MyApplication.local.getFloat(LocalPreferencesHelper.CONTACT_LAT)
                    , MyApplication.local.getFloat(LocalPreferencesHelper.CONTACT_LNG),REQUEST_MAP);

                break;
        }
    }

    private void check() {
        if (mContactNameEt.getText().toString().trim().length() > 0
                && mContactPhoneEt.getText().toString().trim().length() > 0
                && mContactAddressEt.getText().toString().trim().length() > 0) {
            mSubmitBt.setEnabled(true);
        } else {
            mSubmitBt.setEnabled(false);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_MAP) {
                longitude = data.getDoubleExtra(SelectAddressActivity.LNG, -1);
                latitude = data.getDoubleExtra(SelectAddressActivity.LAT, -1);
                address = data.getStringExtra(SelectAddressActivity.ADDRESS);
                mContactAddressEt.setText(address);
                MyApplication.local.saveOrUpdate(LocalPreferencesHelper.CONTACT_LAT,(float)latitude);
                MyApplication.local.saveOrUpdate(LocalPreferencesHelper.CONTACT_LNG,(float)longitude);
            }

        }
    }
}
